<!DOCTYPE html>
<html lang=zh-CN>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="google-site-verification" content="xBT4GhYoi5qRD5tr338pgPM5OWHHIDR6mNg1a3euekI" />
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta property="og:description" content="samuel-24276的博客">
    <meta property="og:type" content="website">
    <meta name="description" content="samuel-24276的博客">
    <meta name="keyword"  content="aircloud">
    <link rel="shortcut icon" href="/myblog/img/favicon.ico">

    <title>
        
        QT学习日记（1）——信号和槽 - samuel-24276的博客 | samuel-24276&#39;s Blog
        
    </title>

    <!-- Custom CSS -->
    
<link rel="stylesheet" href="/myblog/css/aircloud.css">

    
<link rel="stylesheet" href="/myblog/css/gitment.css">

    <!--<link rel="stylesheet" href="https://imsun.github.io/gitment/style/default.css">-->
    <link href="//at.alicdn.com/t/font_620856_28hi1hpxx24.css" rel="stylesheet" type="text/css">
    <!-- ga & ba script hoook -->
    <script></script>
<meta name="generator" content="Hexo 7.1.1"></head>

<body>

<div class="site-nav-toggle" id="site-nav-toggle">
    <button>
        <span class="btn-bar"></span>
        <span class="btn-bar"></span>
        <span class="btn-bar"></span>
    </button>
</div>

<div class="index-about">
    <i> Like what you do, do what you like </i>
</div>

<div class="index-container">
    
    <div class="index-left">
        
<div class="nav" id="nav">
    <div class="avatar-name">
        <div class="avatar ">
            <img src="/myblog/img/avatar.jpg" />
        </div>
        <div class="name">
            <i>samuel-24276</i>
        </div>
    </div>
    <div class="contents" id="nav-content">
        <ul>
            <li >
                <a href="/myblog/">
                    <i class="iconfont icon-shouye1"></i>
                    <span>主页</span>
                </a>
            </li>
            <li >
                <a href="/myblog/tags">
                    <i class="iconfont icon-biaoqian1"></i>
                    <span>标签</span>
                </a>
            </li>
            <li >
                <a href="/myblog/archives">
                    <i class="iconfont icon-guidang2"></i>
                    <span>存档</span>
                </a>
            </li>
            <li >
                <a href="/myblog/collect/">
                    <i class="iconfont icon-shoucang1"></i>
                    <span>收藏</span>
                </a>
            </li>
            <li >
                <a href="/myblog/about/">
                    <i class="iconfont icon-guanyu2"></i>
                    <span>关于</span>
                </a>
            </li>
            
            <li>
                <a id="search">
                    <i class="iconfont icon-sousuo1"></i>
                    <span>搜索</span>
                </a>
            </li>
            
        </ul>
    </div>
    
        <div id="toc" class="toc-article">
    <ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#1-QT%E4%BC%98%E7%82%B9%E5%8F%8A%E5%8E%86%E5%8F%B2"><span class="toc-text">1.QT优点及历史</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#2-%E5%88%9B%E5%BB%BA%E7%AC%AC%E4%B8%80%E4%B8%AAQT%E9%A1%B9%E7%9B%AE"><span class="toc-text">2.创建第一个QT项目</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#2-1-%E9%BB%98%E8%AE%A4%E7%94%9F%E6%88%90%E4%BB%A3%E7%A0%81"><span class="toc-text">2.1.默认生成代码</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-2-%E5%91%BD%E5%90%8D%E8%A7%84%E8%8C%83"><span class="toc-text">2.2.命名规范</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-3-%E5%B8%B8%E7%94%A8%E5%BF%AB%E6%8D%B7%E9%94%AE"><span class="toc-text">2.3.常用快捷键</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#3-%E5%AF%B9%E8%B1%A1%E6%A0%91%E4%B8%8E%E5%9D%90%E6%A0%87%E7%B3%BB"><span class="toc-text">3.对象树与坐标系</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#4-%E4%BF%A1%E5%8F%B7%E4%B8%8E%E6%A7%BD"><span class="toc-text">4.信号与槽</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#4-1-%E8%87%AA%E5%AE%9A%E4%B9%89%E6%8C%89%E9%92%AE%E5%85%B3%E9%97%AD%E7%AA%97%E5%8F%A3"><span class="toc-text">4.1.自定义按钮关闭窗口</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#4-2-%E8%87%AA%E5%AE%9A%E4%B9%89%E4%BF%A1%E5%8F%B7%E5%92%8C%E6%A7%BD%E2%80%94%E2%80%94%E8%80%81%E5%B8%88%E4%B8%8E%E5%AD%A6%E7%94%9F"><span class="toc-text">4.2.自定义信号和槽——老师与学生</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#5-Lambda%E8%A1%A8%E8%BE%BE%E5%BC%8F"><span class="toc-text">5.Lambda表达式</span></a></li></ol>
</div>
    
</div>


<div class="search-field" id="search-field">
    <div class="search-bg" id="search-bg"></div>
    <div class="search-container">
        <div class="search-input">
            <span id="esc-search"> <i class="icon-fanhui iconfont"></i></span>
            <input id="search-input"/>
            <span id="begin-search">搜索</span>
        </div>
        <div class="search-result-container" id="search-result-container">

        </div>
    </div>
</div>

        <div class="index-about-mobile">
            <i> Like what you do, do what you like </i>
        </div>
    </div>
    
    <div class="index-middle">
        <!-- Main Content -->
        


<div class="post-container">
    <div class="post-title">
        QT学习日记（1）——信号和槽
    </div>

    <div class="post-meta">
        <span class="attr">发布于：<span>2019-09-13 20:14:38</span></span>
        
        <span class="attr">标签：/
        
        <a class="tag" href="/myblog/tags/#Qt" title="Qt">Qt</a>
        <span>/</span>
        
        
        </span>
        <span class="attr">访问：<span id="busuanzi_value_page_pv"></span>
</span>
</span>
    </div>
    <div class="post-content no-indent">
        <h1 id="1-QT优点及历史"><a href="#1-QT优点及历史" class="headerlink" title="1.QT优点及历史"></a>1.QT优点及历史</h1><ul>
<li>1.跨平台</li>
<li>2.接口简单，容易上手</li>
<li>3.一定程度上简化了内存回收，市场份额良好</li>
<li>4.支持嵌入式开发<blockquote>
<p>1991年由奇趣公司创建</p>
</blockquote>
</li>
</ul>
<p>成功案例：linux桌面环境，WPS，Vitual Box,谷歌地图，Skype网络电话，屏幕共享软件，VLC播放器</p>
<p>QWidget是QMainWindow和QDialog的父类，QWidget是空窗口，QMainWindow是完备的窗口，QDialog是对话框。</p>
<h1 id="2-创建第一个QT项目"><a href="#2-创建第一个QT项目" class="headerlink" title="2.创建第一个QT项目"></a>2.创建第一个QT项目</h1><h2 id="2-1-默认生成代码"><a href="#2-1-默认生成代码" class="headerlink" title="2.1.默认生成代码"></a>2.1.默认生成代码</h2><ul>
<li>1.pro文件：<code>QT       += core gui</code>默认包含模块core和gui，大于版本4加入模块widgets，Target生成应用程序名称，Source源文件，Headers头文件</li>
<li>2.main.cpp：QApplication应用程序对象，在QT中有且只有一个；Widget创建自定义窗口对象；show()显示窗口；<code>return a.exec();</code>进入消息阻塞机制，让程序阻塞到当前行。</li>
</ul>
<h2 id="2-2-命名规范"><a href="#2-2-命名规范" class="headerlink" title="2.2.命名规范"></a>2.2.命名规范</h2><ul>
<li>1.类名：首字母大写，单词与单词之间首字母大写</li>
<li>2.函数名、变量名：首字母小写，单词与单词之间首字母大写。</li>
</ul>
<h2 id="2-3-常用快捷键"><a href="#2-3-常用快捷键" class="headerlink" title="2.3.常用快捷键"></a>2.3.常用快捷键</h2><ul>
<li>运行：Ctrl + R</li>
<li>编译：Ctrl + D</li>
<li>帮助文档L:F1，两次F1全屏，ESC退出</li>
<li>注释：Ctrl + &#x2F;</li>
<li>字体缩放：Ctrl + 鼠标滚轮</li>
<li>整行代码移动：Ctrl + 上下键</li>
<li>查找：Ctrl + F</li>
<li>自动对齐：Ctrl + I</li>
<li>同名之间的.cpp和.h切换：Ctrl + F4</li>
</ul>
<h1 id="3-对象树与坐标系"><a href="#3-对象树与坐标系" class="headerlink" title="3.对象树与坐标系"></a>3.对象树与坐标系</h1><p>在Qt中创建对象时会提供一个Parent对象指针，下面来解释这个对象指针是干什么的。</p>
<ul>
<li>1.QObject是以对象树的形式组织起来的。<ul>
<li>当你创建一个QObject对象时，会看到QObject构造函数接收一个QObject指针做参数，这个参数就是Parent，也就是父对象指针。这相当于，<strong>在创建QObject对象时，可以提供一个其父对象，我们创建的这个QObject对象会自动添加到其父对象的children()列表</strong>。</li>
<li><strong>当父对象析构时，这个列表中的所有对象都会被析构（注意，这里的父对象不是继承意义上的父类）</strong>。这种机制在GUI设计中十分有用，例如一个按钮有一个QShortCut（快捷键）对象作为其子对象，当我们删除按钮时，这个快捷键理应被删除。</li>
</ul>
</li>
<li>2.Qwidget是能够在屏幕上显示的一切组件的父类。<ul>
<li><strong>Qwidget继承自QObject，因此继承了这种对象树关系，一个孩子自动成为父组件的一个子组件</strong>。因此，它会显示在父组件的坐标系统中，被父组件的边界剪裁。例如，当用户尝试关闭一个对话框时，应用程序将其删除，那么，我们希望这个对话框的按钮、图标等应该一起被删除，事实如此。</li>
<li>当然，<strong>我们也可以自己删除子对象，它们会自动从其父对象列表中删除</strong>。例如，我们删除一个工具栏时，其所在的主窗口会自动将工具栏从其子对象列表中删除，并自动调整屏幕显示。</li>
</ul>
</li>
<li>3.Qt引入对象树的概念，一定程度上解决了内存问题。<ul>
<li>在创建对象时，如果父类是QObject或QObject的派生类，这个对象不用管理释放了。</li>
<li>构造顺序和释放顺序是相反的，释放时先释放子类，后释放父类，但显示上会还是父先子后。</li>
</ul>
</li>
<li>4.坐标系：左上角为（0，0）点，x是横向，y是纵向，向右为x正向，向下为y正向。</li>
</ul>
<h1 id="4-信号与槽"><a href="#4-信号与槽" class="headerlink" title="4.信号与槽"></a>4.信号与槽</h1><p>connect(信号发送者，发送的信号，信号接收者，处理函数（槽函数）)</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//需求：点击自定义按钮，把窗口关掉</span></span><br><span class="line"><span class="comment">//利用connect进行链接：参数1 信号发送者， 参数2 发送的信号， 参数3 信号接收者， 参数4 处理信号（槽函数）</span></span><br><span class="line"><span class="comment">//connect(mybtn, &amp;QPushButton::clicked, this, &amp;QWidget::close);</span></span><br><span class="line"><span class="comment">//调用信号时使用子类是最直接的</span></span><br><span class="line"><span class="built_in">connect</span>(mybtn, &amp;MyPushButton::clicked, <span class="keyword">this</span>, &amp;Widget::close);</span><br></pre></td></tr></table></figure>
<p>Ctrl + Shift + A 调用帮助文档（自己设置的）查找信号，自定义按钮mybtn为信号发送者，其信号查找需查找QPushButton，发现其继承了其他基类的signal，因此需要到基类里查找信号，找到点击信号clicked，需要引用&amp;。同理，接收端是整个窗口this，需要进行的处理是关闭，查找类QWidget，在contents里找到槽函数public slots，点进去可以看到子函数close(),调用即可。</p>
<blockquote>
<p>优点：松散耦合，发送端与接收端是松散的，通过connect将其连接起来，耦合了。</p>
</blockquote>
<h2 id="4-1-自定义按钮关闭窗口"><a href="#4-1-自定义按钮关闭窗口" class="headerlink" title="4.1.自定义按钮关闭窗口"></a>4.1.自定义按钮关闭窗口</h2><p>widget.cpp的析构函数代码如下：</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line">ui-&gt;<span class="built_in">setupUi</span>(<span class="keyword">this</span>);</span><br><span class="line">QPushButton *btn = <span class="keyword">new</span> QPushButton;  <span class="comment">//创建按钮对象</span></span><br><span class="line"><span class="comment">//btn-&gt;show();                         //show()函数默认是顶层方式弹出窗口</span></span><br><span class="line">btn-&gt;<span class="built_in">setParent</span>(<span class="keyword">this</span>);</span><br><span class="line"><span class="comment">//显示文本</span></span><br><span class="line">btn-&gt;<span class="built_in">setText</span>(<span class="string">&quot;按钮&quot;</span>);     <span class="comment">//若中文乱码，则工具-&gt;选项-&gt;文本编辑器-&gt;默认编码格式修改为UTF-8</span></span><br><span class="line"><span class="comment">//创建第二个按钮</span></span><br><span class="line">QPushButton *btn2 = <span class="keyword">new</span> <span class="built_in">QPushButton</span>(<span class="string">&quot;按钮2&quot;</span>);</span><br><span class="line">btn2-&gt;<span class="built_in">setParent</span>(<span class="keyword">this</span>);</span><br><span class="line"><span class="comment">//移动按钮</span></span><br><span class="line">btn2-&gt;<span class="built_in">move</span>(<span class="number">100</span>,<span class="number">100</span>);</span><br><span class="line"><span class="comment">//设置窗口大小</span></span><br><span class="line"><span class="keyword">this</span>-&gt;<span class="built_in">resize</span>(<span class="number">600</span>, <span class="number">400</span>);</span><br><span class="line"><span class="comment">//设置窗口标题</span></span><br><span class="line"><span class="keyword">this</span>-&gt;<span class="built_in">setWindowTitle</span>(<span class="string">&quot;第一个项目&quot;</span>);</span><br><span class="line"><span class="comment">//设置固定窗口大小</span></span><br><span class="line"><span class="keyword">this</span>-&gt;<span class="built_in">setFixedSize</span>(<span class="number">600</span>, <span class="number">400</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">//创建自定义按钮</span></span><br><span class="line">MyPushButton * mybtn = <span class="keyword">new</span> MyPushButton;</span><br><span class="line">mybtn-&gt;<span class="built_in">setText</span>(<span class="string">&quot;自定义按钮&quot;</span>);</span><br><span class="line">mybtn-&gt;<span class="built_in">move</span>(<span class="number">100</span>,<span class="number">50</span>);</span><br><span class="line">mybtn-&gt;<span class="built_in">setParent</span>(<span class="keyword">this</span>);         <span class="comment">//设置了父窗口，将控件放入到对象树上，不用管理释放了</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//需求：点击自定义按钮，把窗口关掉</span></span><br><span class="line"><span class="comment">//利用connect进行链接：参数1 信号发送者， 参数2 发送的信号， 参数3 信号接收者， 参数4 处理信号（槽函数）</span></span><br><span class="line"><span class="comment">//connect(mybtn, &amp;QPushButton::clicked, this, &amp;QWidget::close);</span></span><br><span class="line"><span class="built_in">connect</span>(mybtn, &amp;MyPushButton::clicked, <span class="keyword">this</span>, &amp;Widget::close);</span><br></pre></td></tr></table></figure>

<h2 id="4-2-自定义信号和槽——老师与学生"><a href="#4-2-自定义信号和槽——老师与学生" class="headerlink" title="4.2.自定义信号和槽——老师与学生"></a>4.2.自定义信号和槽——老师与学生</h2><p>新建SignalAndSlot项目，添加Teacher和Student类，在其头文件下自定义信号和槽函数，代码如下：</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">Teacher</span> : <span class="keyword">public</span> QObject</span><br><span class="line">&#123;</span><br><span class="line">    Q_OBJECT</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">explicit</span> <span class="title">Teacher</span><span class="params">(QObject *parent = <span class="literal">nullptr</span>)</span></span>;</span><br><span class="line"><span class="comment">//自定义信号写到signals:下</span></span><br><span class="line">signals:</span><br><span class="line">    <span class="function"><span class="type">void</span> <span class="title">hungry</span><span class="params">()</span></span>;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">Student</span> : <span class="keyword">public</span> QObject</span><br><span class="line">&#123;</span><br><span class="line">    Q_OBJECT</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">explicit</span> <span class="title">Student</span><span class="params">(QObject *parent = <span class="literal">nullptr</span>)</span></span>;</span><br><span class="line"></span><br><span class="line">signals:</span><br><span class="line"></span><br><span class="line"><span class="comment">//自定义槽函数，写到public slots:下，或者全局函数，或者public下，或者lambda表达式</span></span><br><span class="line"><span class="keyword">public</span> slots:</span><br><span class="line">    <span class="function"><span class="type">void</span> <span class="title">treat</span><span class="params">()</span></span>;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<p>之后在student.cpp里实现treat(),代码如下：</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="type">void</span> <span class="title">Student::treat</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="built_in">qDebug</span>()&lt;&lt;<span class="string">&quot;请老师吃饭&quot;</span>&lt;&lt;endl;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>转入widget.h文件，声明老师和学生：</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">Teacher *zt;</span><br><span class="line">Student *st;</span><br></pre></td></tr></table></figure>
<p>并在widget.cpp里实现：</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//创建老师和学生对象，并将this声明为他们的父窗口</span></span><br><span class="line">zt = <span class="keyword">new</span> <span class="built_in">Teacher</span>(<span class="keyword">this</span>);</span><br><span class="line">st = <span class="keyword">new</span> <span class="built_in">Student</span>(<span class="keyword">this</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">//连接信号和槽</span></span><br><span class="line"><span class="built_in">connect</span>(zt, &amp;Teacher::hungry, st, &amp;Student::treat);</span><br></pre></td></tr></table></figure>
<p>但是运行后发现学生并没有请老师吃饭，就像之前的点击自定义按钮关闭窗口，这里并没有点击按钮的信号发送，因此学生并没有请老师吃饭，需要在widget.h补充classIsOver()函数,并在widget.cpp中实现：</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="type">void</span> <span class="title">Widget::classIsOver</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="comment">//自定义信号触发emit</span></span><br><span class="line">    emit zt-&gt;<span class="built_in">hungry</span>();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>并且在widget.cpp的构造函数中调用：<br><code>classIsOver();</code>即可请老师吃饭。  </p>
<ul>
<li><p>1.自定义信号写法：</p>
<ul>
<li>1.写在signals:下面</li>
<li>2.返回void</li>
<li>3.<strong>信号只需要声明，不需要实现</strong></li>
<li>4.可以发生重载</li>
</ul>
</li>
<li><p>2.自定义槽函数写法：</p>
<ul>
<li>1.自定义槽函数，写到public slots:下，或者全局函数，或者Public下，或者lambda表达式</li>
<li>2.返回void</li>
<li>3.<strong>槽函数需要声明，也需要实现</strong></li>
<li>4.可以发生重载</li>
</ul>
</li>
<li><p>3.信号与信号连接触发事件，widget.cpp代码如下：</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><span class="line">Widget::<span class="built_in">Widget</span>(QWidget *parent)</span><br><span class="line">    : <span class="built_in">QWidget</span>(parent)</span><br><span class="line">&#123;</span><br><span class="line">    <span class="comment">//创建老师和学生对象，并将this声明为他们的父窗口</span></span><br><span class="line">    zt = <span class="keyword">new</span> <span class="built_in">Teacher</span>(<span class="keyword">this</span>);</span><br><span class="line">    st = <span class="keyword">new</span> <span class="built_in">Student</span>(<span class="keyword">this</span>);</span><br><span class="line"></span><br><span class="line">    <span class="comment">//连接信号和槽</span></span><br><span class="line">    <span class="comment">//connect(zt, &amp;Teacher::hungry, st, &amp;Student::treat);</span></span><br><span class="line">    <span class="comment">//带参的信号和槽函数</span></span><br><span class="line">    <span class="comment">//指针-&gt;地址</span></span><br><span class="line">    <span class="comment">//函数指针-&gt;函数地址</span></span><br><span class="line">    <span class="built_in">void</span> (Teacher:: * teacherSignals )(QString) = &amp;Teacher::hungry;</span><br><span class="line">    <span class="built_in">void</span> (Student:: * studentSlot )(QString) = &amp;Student::treat;</span><br><span class="line">    <span class="built_in">connect</span>(zt, teacherSignals, st, studentSlot);</span><br><span class="line">    <span class="comment">//重载后无参的信号和槽函数</span></span><br><span class="line">    <span class="built_in">void</span> (Teacher:: * teacherSignals1 )() = &amp;Teacher::hungry;</span><br><span class="line">    <span class="built_in">void</span> (Student:: * studentSlot1 )() = &amp;Student::treat;</span><br><span class="line">    <span class="built_in">connect</span>(zt, teacherSignals1, st, studentSlot1);</span><br><span class="line"></span><br><span class="line">    <span class="comment">//信号和槽可以断开连接</span></span><br><span class="line">    <span class="built_in">disconnect</span>(zt, teacherSignals1, st, studentSlot1);</span><br><span class="line"></span><br><span class="line">    <span class="comment">//信号和槽的扩展</span></span><br><span class="line">    <span class="comment">//1.信号可以连接信号</span></span><br><span class="line">    QPushButton * btn = <span class="keyword">new</span> <span class="built_in">QPushButton</span>(<span class="keyword">this</span>);</span><br><span class="line">    btn-&gt;<span class="built_in">setText</span>(<span class="string">&quot;下课&quot;</span>);</span><br><span class="line">    <span class="keyword">this</span>-&gt;<span class="built_in">resize</span>(<span class="number">600</span>, <span class="number">400</span>);</span><br><span class="line">    <span class="built_in">void</span> (Teacher:: * teacherSignals2 )() = &amp;Teacher::hungry;</span><br><span class="line">    <span class="built_in">void</span> (Student:: * studentSlot2 )() = &amp;Student::treat;</span><br><span class="line">    <span class="built_in">connect</span>(zt, teacherSignals2, st, studentSlot2);</span><br><span class="line">    <span class="built_in">connect</span>(btn, &amp;QPushButton::clicked, zt, teacherSignals2);</span><br><span class="line">    <span class="built_in">classIsOver</span>();</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">Widget::classIsOver</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="comment">//自定义信号触发emit</span></span><br><span class="line">    <span class="comment">//emit zt-&gt;hungry();</span></span><br><span class="line">    emit zt-&gt;<span class="built_in">hungry</span>(<span class="string">&quot;宫保鸡丁&quot;</span>);</span><br><span class="line">    <span class="comment">//emit zt-&gt;hungry();</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p>4.一个信号连接多个槽函数，在 <code>classIsOver();</code> 之前加一句<br><code>connect(btn, &amp;QPushButton::clicked, this, &amp;Widget::close);</code>，这样，一个按钮就可以实现两个功能了。</p>
</li>
<li><p>5.多个信号连接同一个槽函数</p>
</li>
<li><p>6.信号和槽函数的参数类型必须一一对应，参数个数可以不一致，信号的参数可以多于槽函数的参数个数</p>
</li>
<li><p>7.信号和槽可以通过disconnect断开连接</p>
</li>
<li><p>8.QT4写法：<code>connect(zt, SIGNAL(teacherSignals(QString)), st, SLOT(studentSlot(int)) );</code>,它不会做参数类型检测，若类型出错不会提示，因此不提倡这种写法。</p>
</li>
<li><p>9.emit关键字发送信号。</p>
</li>
</ul>
<h1 id="5-Lambda表达式"><a href="#5-Lambda表达式" class="headerlink" title="5.Lambda表达式"></a>5.Lambda表达式</h1><p>用于定义并创建匿名的函数对象，结构如下所示：</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">[capture](param) <span class="keyword">mutable</span>-&gt;<span class="keyword">return</span>-type</span><br><span class="line">&#123;</span><br><span class="line">  statement</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<ul>
<li>1.[]标识lambda表达式的开始，不能省略。函数对象参数是传递给编译器自动生成的函数对象类的构造函数的。函数对象参数只能使用那些定义到Lambda为止时Lambda所在作用范围内可见的局部变量（包括Lambda所在类的this）。函数对象参数有以下形式：<ul>
<li>1.空。没有使用任何函数对象参数。</li>
<li>2.&#x3D;。函数体内可以使用Lambda表达式所在作用范围内所有可见的局部变量（作用范围是它所在的相同层次，包括lambda类的this），并且是值传递方式（相当于编译器自动为我们按值传递了所有局部变量）。QT中一般用按值传递。</li>
<li>3.&amp;。函数体内可以使用Lambda表达式所在作用范围内所有可见的局部变量（作用范围是它所在的相同层次，包括lambda类的this），并且是引用传递方式（相当于编译器自动为我们按引用传递了所有局部变量）。</li>
</ul>
</li>
<li>2.操作符重载函数参数：<br>标识重载的()操作符的参数，没有参数时，这部分可以省略，参数可以通过按值和按引用两种方式进行传递。</li>
<li>3.可修改标识符：<br>mutable声明，这部分可以省略。按值传递函数对象参数时，加上mutable修饰符后，可以修改按值传递进来的拷贝（注意修改的是拷贝，而不是值本身）。</li>
<li>4.函数返回值：<br>-&gt;返回值类型，标识函数返回值的类型，当返回值为void时，或者函数体中只有一处return的地方（此时编译器可以自动推断出返回值的类型）时，这部分可以省略。</li>
<li>5.函数体：<br>{}，标识函数的实现，这部分不能省略，但可以为空。</li>
</ul>
<hr>

        
            <div class="donate-container">
    <div class="donate-button">
        <button id="donate-button">赞赏</button>
    </div>
    <div class="donate-img-container hide" id="donate-img-container">
        <img id="donate-img" src="" data-src="/myblog/img/donate.jpg">
        <p> 感谢鼓励 </p>
    </div>
</div>
        
        <br />
        <div id="comment-container">
        </div>
        <div id="disqus_thread"></div>
        <div id="lv-container"></div>
        <div class="giscus"></div>
    </div>
</div>

    </div>
</div>


<footer class="footer">
    <ul class="list-inline text-center">
        
        

        

        

        
        <li>
            <a target="_blank"  href="https://github.com/samuel-24276">
                            <span class="fa-stack fa-lg">
                                <i class="iconfont icon-github"></i>
                            </span>
            </a>
        </li>
        

        

    </ul>
    
    <p>
        <span>/</span>
        
        <span><a target="_blank" rel="noopener" href="https://github.com/samuel-24276">samuel-24276&#39;s Page</a></span>
        <span>/</span>
        
        <span><a target="_blank" rel="noopener" href="https://beian.miit.gov.cn/">北京-未备案</a></span>
        <span>/</span>
        
        <span><a href="#">返回顶部</a></span>
        <span>/</span>
        
    </p>
    
    <p>
        <span id="busuanzi_container_site_pv">
            <span id="busuanzi_value_site_pv"></span>PV
        </span>
        <span id="busuanzi_container_site_uv">
            <span id="busuanzi_value_site_uv"></span>UV
        </span>
        Created By <a target="_blank" rel="noopener" href="https://hexo.io/">Hexo</a>  Theme <a target="_blank" rel="noopener" href="https://github.com/aircloud/hexo-theme-aircloud">AirCloud</a></p>
</footer>




</body>

<script>
    // We expose some of the variables needed by the front end
    window.hexo_search_path = "search.json"
    window.hexo_root = "/myblog/"
    window.isPost = true
</script>
<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>

<script src="/myblog/js/index.js"></script>

<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>






    <script type="text/javascript">
       (function() {
           if (typeof LivereTower === 'function') { return; }

           var j, d = document.getElementById('lv-container');

           d.setAttribute('data-id','city');
           d.setAttribute('data-uid' , 'MTAyMC80OTA5OC8yNTU5Mw==');

           j = document.createElement('script');
           j.src = 'https://cdn-city.livere.com/js/embed.dist.js';
           j.async = true;

           d.appendChild(j);
       })();
    </script>
    <noscript>为正常使用来必力评论功能请激活JavaScript</noscript>
    </div>

</html>
